<?php

namespace App\Controller;

use App\Request\User\AccountRegisterRequest;
use App\Request\User\MobileRegisterRequest;
use App\Service\UserService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\PostMapping;

#[Controller('/')]
class UserController extends AbstractController
{
    #[Inject]
    protected UserService $userService;

    #[Inject]
    protected AccountRegisterRequest $accountRegisterRequest;

    #[Inject]
    protected MobileRegisterRequest $mobileRegisterRequest;

    #[PostMapping("register-account")]
    public function registerByAccount(){
        $this->accountRegisterRequest->validateResolved();
        $data = $this->accountRegisterRequest->validated();
        $this->userService->registerByAccount($data['account'], $data['password']);
        return [
            'code' => 200,
            'msg' => 'ok'
        ];
    }

    #[PostMapping("register-mobile")]
    public function registerByMobile(){
        $this->mobileRegisterRequest->validateResolved();
        $data = $this->mobileRegisterRequest->validated();
        $this->userService->registerByMobile($data['mobile'], $data['code']);
        return [
            'code' => 200,
            'msg' => 'ok'
        ];
    }


    #[PostMapping("/sms")]
    public function sms(){
        $mobile = $this->request->post('mobile');
        if(empty($mobile)){
            return [
                'code' => 400,
                'msg' => '手机号不能为空'
            ];
        }
        $this->userService->sendSms($mobile);
        return [
            'code' => 200,
            'msg' => 'ok'
        ];
    }
}